<template>
  <div class="TVMPiaoKuanCYXX">
    <basic-container>
      <avue-crud
        ref="crud"
        :option="option"
        :table-loading="listLoading"
        :data="list"
        :page.sync="page"
        @search-change="handleFilter"
        @selection-change="selectionChange"
        @refresh-change="getList"
        @on-load="getList">
        <template slot="searchMenu" slot-scope="{row,size}">
          <el-button :size="size" @click="()=>{setSearchFiledsEmpty(row);setSearchFiledsEmpty(searchForm)}" icon="el-icon-delete">清空</el-button>
        </template>


        <template slot-scope="scope" slot="lineCodeSearch">
          <el-select v-model="searchForm.lineCode" placeholder="请选择" style="width: 150px" @change="changeLine" filterable>
            <el-option
              v-for="(item,index) in lineCodes"
              :key="index"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </template>
        <template slot-scope="scope" slot="stationCodeSearch">
          <el-select v-model="searchForm.stationCode" placeholder="请选择" style="width: 150px" filterable>
            <el-option
              v-for="(item,index) in stationCodes"
              :key="index"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </template>
        <template slot-scope="scope" slot="tvmOrderSearch">
          <el-select v-model="searchForm.tvmOrder" placeholder="请选择" style="width: 150px" filterable >
            <el-option
              v-for="(item,index) in tvmCodes"
              :key="index"
              :label="item.tvmCode"
              :value="item.tvmCode">
            </el-option>
          </el-select>
        </template>


        <template slot-scope="scope" slot="conductorPersonCodeSearch">
          <el-select v-model="searchForm.conductorPersonCode" placeholder="请选择" style="width: 150px" filterable>
            <el-option
              v-for="(item,index) in userList"
              :key="index"
              :label="item.username"
              :value="item.userCode">
            </el-option>
          </el-select>
        </template>
        <template slot-scope="scope" slot="menu">
          <el-button type="text" size="small" v-if="scope.row.kyReplyStatus!=1" plain @click.stop="previewEditor(scope.row,2)">编辑</el-button>
          <el-button type="text" size="small"  plain @click.stop="previewEditor(scope.row,1)">预览</el-button>
        </template>
        <template slot="menuRight">
          <el-button type="primary"  size="mini" @click="batchFeedback">批量回馈差异原因</el-button>
          <el-button type="primary"  size="mini" @click="submit">提交</el-button>
          <el-button type="primary"  size="mini" @click="exportData">导出</el-button>
        </template>
      </avue-crud>
    </basic-container>
    <el-dialog :visible="isShowDiaBatchFeedback" @close="closed">
      <avue-form :option="optionForm" v-model="formData" @submit="handleSubmit">
        <template slot-scope="scope" slot="kyReplyReason">
          <el-select v-model="kyReplyReason" placeholder="请选择" style="width: 150px">
            <el-option
              v-for="(item,index) in kyReplyReasonList"
              :key="index"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </template>
        <template slot-scope="scope" slot="kyReplyDetail">
          <el-input type="textarea" v-model="kyReplyDetail"></el-input>
        </template>
        <template slot-scope="scope" slot="menuForm" >
          <el-button type="primary" @click="handleSubmit" >保存</el-button>
          <el-button @click="closed" >返回</el-button>
        </template>
      </avue-form>
    </el-dialog>
    <HuikuiInfo ref="HuikuiInfo" v-if="isShowDia"></HuikuiInfo>
  </div>

</template>

<script>
  import {getForm, postForm} from '@/api/gen/gen'
  import {mapGetters} from 'vuex';
  import HuikuiInfo from './HuikuiInfo';
  import {exportDataToExcel} from "../../../../api/subwayRat/exportSelf";
  import { handleExcelResponse } from "@/util/util";
  import {
    setSearchFiledsEmpty,
    getSwPmDeviceOperationCodeList,
    getSysUserToLS,
    getSysDictItemListToDict,
    getStationByCode,
    getLineAndStationByCode,
    getLineCodes, getgetStationCodes, getSwPmTvmCodeList
  } from "../../../../api/subwayRat/commonCheck";
  import {tableOption,get_xw_SwAuTvmMoneyAssistList,updateSwAuTvmMoneyAssist,updateSwAuTvmMoneyAssistToStatus,exportTvmMoneyAssist
  } from '../../../../api/stationTiecketManager/shuJuXieChaGuanLi/TVM/TVMPiaoKuanCYXX';
  import ElDialog from "../../../../../node_modules/element-ui/packages/dialog/src/component";
  import ElInput from "../../../../../node_modules/element-ui/packages/input/src/input";
  export default {
    name: 'TVMPiaoKuanCYXX',
    components:{
      ElInput,
      ElDialog,
      HuikuiInfo
    },
    data() {
      return {
        kyReplyReason:'',
        kyReplyDetail:'',
        isShowDia:false,
        isShowDiaBatchFeedback:false,
        option: tableOption,
        optionForm: {},
        lineCodes:[],
        stationCodes:[],
        tvmCodes:[],
        formData: {},
        listLoading: true,
        selectKeys:[], //存放对像
        selectKeyVals:[], //存放值
        searchForm:{},
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 20, // 每页显示多少条,
          isAsc: false// 是否倒序
        },
        ids : [],
        list : [],
        userList:[],
        showType : 0,
        equipmentNumberList : [],
        kyReplyReasonList:[]
      }
    },
    computed: {
      ...mapGetters(['permissions'])
    },
    watch: {
    },
    mounted() {
      let param={};
      getSwPmTvmCodeList(param).then((r)=>{
        this.tvmCodes=r.data;
      });
      let p = {
        line_code : this.$store.getters.userInfo.lineCode,
        station_code : this.$store.getters.userInfo.stationCode
      };
      getSysUserToLS(p).then((response)=>{
        this.userList = response.data.data;
      });
      getLineCodes().then(res=>{ //获取线路
        this.lineCodes=res;
      });
    },
    created() {
      this.getSheBei();
      getSysDictItemListToDict({"type":"KY_REPLY_REASON"}).then((response)=>{
        this.kyReplyReasonList = response.data.data;
      });
    },
    methods: {
      setSearchFiledsEmpty,
      async handleSubmit(){
        if('' == this.kyReplyReason || '' == this.kyReplyDetail){
          this.$message.warning("请填写回馈内容！");
          return;
        }
        let p = {
          ids : JSON.stringify(this.selectKeyVals),
          kyReplyReason : this.kyReplyReason,
          kyReplyDetail : this.kyReplyDetail
        };
        await updateSwAuTvmMoneyAssist(p);
        this.$message.success("成功");
        this.isShowDiaBatchFeedback = false;
        this.getList();
      },
      async submit(){
        if(this.selectKeys.length<=0){
          this.$message.warning("请选择要回馈的数据！");
          return;
        }
        for(let i=0;i<this.selectKeys.length;i++){
          if(1 == this.selectKeys[i].kyReplyStatus){
            this.$message.warning("勾选数据中存在客运已回馈数据！");
            return
          }
          // if(2 == this.selectKeys[i].replyReason){
          //   this.$message.warning("勾选数据中存在机电回馈原因为人为原因数据！");
          //   return
          // }
        }
        let ids=[];
        for(let i=0;i<this.selectKeys.length;i++){
          ids.push(this.selectKeys[i].id);
        }
        let messageLSList = [];
        for(let i=0;i<this.selectKeys.length;i++){
          let data = await getLineAndStationByCode({"lineCode":this.selectKeys[i].lineCode, "code":this.selectKeys[i].stationCode});
          messageLSList.push({
            "content":"车站：" + data.data[0].name,
            "lineCode":this.selectKeys[i].lineCode,
            "stationCode":this.selectKeys[i].stationCode
          })
        }
        let p = {
          messageLSList:JSON.stringify(messageLSList),
          //title:this.$store.getters.tag.label,
	  title:'数据协查管理-TVM票款差异协查',
          msgType:"提交",
          type:"TVMPiaoKuanCYXX_001",
          ids:ids
        };
        await updateSwAuTvmMoneyAssistToStatus(p).then((response)=>{
        });
        this.$message.success("提交成功！");
        this.getList();
      },
      batchFeedback(){
        if(this.selectKeys.length<=0){
          this.$message.warning("请选择要回馈的数据！");
          return;
        }
        for(let i=0;i<this.selectKeys.length;i++){
          if(1 == this.selectKeys[i].kyReplyStatus){
            this.$message.warning("勾选数据中存在客运已回馈数据！");
            return
          }
        }
        let ids=[];
        for(let i=0;i<this.selectKeys.length;i++){
          ids.push(this.selectKeys[i].id);
        }
        this.ids = ids;
        this.optionForm={
          submitBtn:false,
          emptyBtn:false,
          group:[{
            icon:'',
            label: '',
            // collapse:false,
            prop: 'group',
            column: [{
              label: '归属客运专业回馈差异原因',
              prop: 'kyReplyReason',
              formslot : true,
              labelWidth:130
            },{
              label: '客运专业详细描述',
              prop: 'kyReplyDetail',
              formslot : true,
              type:'textarea',
              labelWidth:130
            }]
          }]
        };
        this.isShowDiaBatchFeedback = true;
      },
      selectionChange(list){
        this.selectKeys=list;
        this.selectKeyVals=[];
        for(let i=0;i<list.length;i++){
          this.selectKeyVals.push(list[i].id);
        }
      },
      // async exportData(){ // 导出
      //   this.searchForm.pageNo=1;
      //   this.searchForm.pageSize=1000000000;
      //   if(this.selectKeys.length>0){
      //     let newData = JSON.parse(JSON.stringify(this.selectKeys));
      //     exportDataToExcel(newData,this.option.column,'TVM分钱箱票款差异协查');
      //   }else {
      //     // this.searchForm.replyReason = "2";
      //     // this.searchForm.jdReplyStatus = "1";
      //     this.searchForm.lineCode = this.$store.getters.userInfo.lineCode;
      //     this.searchForm.stationCode = this.$store.getters.userInfo.stationCode;
      //     let res=await get_xw_SwAuTvmMoneyAssistList(this.searchForm);
      //     let data = res.data.data;
      //     exportDataToExcel(data,this.option.column,'TVM分钱箱票款差异协查');
      //   }
      // },
      async exportData(){ //导出
        let loading = this.$loading({
          lock: true,
          text: "Loading",
          spinner: "el-icon-loading",
          background: "rgba(0, 0, 0, 0.7)"
        })
        try {
          if(this.selectKeys.length>0){
            const res = await exportTvmMoneyAssist({ids:this.selectKeyVals.join(","), tag:"keyun"});
            handleExcelResponse(res);
          }else{
            this.searchForm.kyAssistInvestStatus = "1";//
            this.searchForm.tag = "keyun";//导出客运标识
            const res = await exportTvmMoneyAssist(this.searchForm);
            handleExcelResponse(res);
          }
        } finally {
          loading.close();
        }
      },
      previewEditor(row,type){
        this.showType = type;
        this.isShowDia=true;
        this.$nextTick(()=>{
          this.$refs.HuikuiInfo.recallShow(row,type);
        })
      },
      async getSheBei(){
        let res=[];
        await getSwPmDeviceOperationCodeList({lineCode:this.$store.getters.userInfo.lineCode,stationCode:this.$store.getters.userInfo.stationCode}).then(r=>{
          res=r.data.data;
        });
        this.equipmentNumberList=res;
        return res;
      },
      getList() {
        this.listLoading = true;

        Object.assign(this.searchForm,this.page);
        // this.searchForm.replyReason = "2";
        // this.searchForm.jdReplyStatus = "1";
        this.searchForm.kyAssistInvestStatus = "1";
        this.searchForm.lineCode = this.$store.getters.userInfo.lineCode;
        this.searchForm.stationCode = this.$store.getters.userInfo.stationCode;
        get_xw_SwAuTvmMoneyAssistList(this.searchForm).then((response)=>{
          this.list = response.data.data;
          this.listLoading = false;
          this.page.total=response.data.total;
          // this.list = response.data.data.list;
          // this.listLoading = false;
          // this.page.total=response.data.data.pageInfo.total;
        });
      },
      changeLine(v){ //改变线路时
        getgetStationCodes(v).then(res=>{
          this.stationCodes=res;
          this.searchForm.stationCode='';
        })
      },
      async changeStation(){ //改变车站
        let param={};
        param.lineCode=1;
        param.stationCode=15;
        let r = await getSwPmTvmCodeList(param);
        this.tvmCodes=r.data;
        debugger
      },
      handleFilter(params, done) {
        Object.assign(this.searchForm,params);
        this.getList();
        done()
      },
      closed(){
          this.isShowDiaBatchFeedback=false
      }
    }
  }
</script>
